'''urllib下载'''
import urllib.request

# ●下载网页
url = 'http://www.baidu.com'
# urllib.request.urlretrieve(网络地址, 文件名称)
urllib.request.urlretrieve(url, 'baidu.html')


# ●下载图片
urlImg = 'http://p3.qhimg.com/bdm/384_237_0/t01a2c69f5b7eeac889.jpg'
urllib.request.urlretrieve(urlImg, 'D:\\system\\deve\python\\test\\testimg1.jpg')
# 下载千库网图片，由于页面无法右键，只能通过查看html
urlImg2 = 'https://bpic.588ku.com/element_origin_min_pic/23/03/29/6615159b37dfbdc951de7a3c76008f5d.jpg'
urllib.request.urlretrieve(urlImg2, 'D:\\system\\deve\python\\test\\testimg2.jpg')


# ●下载视频,注意下载西瓜视频需要在F12检查模式中查找接口
urlVideo= 'https://v3-xg-web-pc.ixigua.com/4f10e26363dff5feeb7bcb59bc53aa04/6432380d/video/tos/cn/tos-cn-ve-4/a8cf9be43b8e4a028bfddf2a46d6a6b7/?a=1768&ch=0&cr=0&dr=0&er=0&cd=0%7C0%7C0%7C0&cv=1&br=807&bt=807&cs=0&ds=3&ft=_z7ehvvBQadUL8ym8Z.BOMzJ4lcfvE1W2bLqNHjb8Zmka&mime_type=video_mp4&qs=0&rc=ZTlpZGQ1Zjo8aGZmOGZkNkBpamR5c3J1NzprNDMzNDczM0AxLTVhYi0yXi4xL18yY2I2YSNzby4zLW9ubWhgLS1kLWFzcw%3D%3D&l=202304091057256023F2B8A9611FA91992&btag=28000&__vid=v0d004g10000c1khp7q4rr0o6p1vravg'
urllib.request.urlretrieve(urlVideo, 'D:\\system\\deve\python\\test\\test.mp4')


# ●下载图片需要请求头headers 案例
## 图片访问路径
url = 'https://tools.kalvinbg.cn/comm/static/image/%E6%9C%89%E8%B6%A3%E5%AE%9E%E7%94%A8%E7%A7%91%E6%8A%80%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8%28%E5%85%AC%E7%AB%A0%29_ne7_%E6%B0%B4%E5%8D%B0.png'
headers = {
    "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
    "Cookie":
        "BAIDU_SSP_lcr=https://www.baidu.com/link?url=Sq4OhzElmC8cu9GIxCR0v_7LdbXlZdxdWvmvvioaI2lhDvImYVAXRd8-_R89hG-nZM8R-dw9Hxd2hZvRg0-abK&wd=&eqid=c40acc970004b26700000003644a3746; __bid_n=187c1e8fbbe2af28234207; FPTOKEN=ugBFRjYzT8BqpDraMoeEyU0DKkYGLopa/RseWt95TaqUqGkJ9xrhOsqrrTBSiLqQvnDnnhsK2N+Q1r98AvROEFlQcfZOVmsVMN77KjQEY0eZ9p9DKpZcvzMQJYDD7sJUnQ/0bcl7fw3t4nS+KRtfs0rCJ+btZl8I41g3pZfwS33YjTRWXJJ3giVu0twpvwB/ov6hKbx9vv+iXBXnifmoMgmRrRWOrh8QDGKvxVSjtMOPUcwOBcxfWaV2hebj0Hrh0ouZhAhsPz9ILYlATcAgfEf3CNCU3LPQ44lp11POzTNrjzXKADfvBjXTFJG0Wfd4H1PM2eRBsMnQ5Ej6HGFFw6VxnCvTZ1xByllcquw6lnk/Y/cyBIflr4HnTsTzvv0PIT2YpKwbkbt7i9S2lqmvZQ==|j6EcvPiAL0WUJVSTovHBtabEOPQEQ5GpavmRliKWNFI=|10|bd4250bff4ee8bf540266b0c36af6b33; __qc_wId=845; Hm_lvt_d944c2103ca675bd03014e766c6f45f4=1682585487; BAIDU_SSP_lcr=https://www.baidu.com/link?url=uFWBsempqt_jCZ8gcoTyLMzxWo176G_xvTYw1BawknrA7HJobQSsbkaYJEkfvQKq&wd=&eqid=e2aa2ad600008f9a00000003644a3992; Hm_lpvt_d944c2103ca675bd03014e766c6f45f4=1682586122"
}
request = urllib.request.Request(url=url, headers=headers)
response = urllib.request.urlopen(request)
content = response.read()
## 将图片下载到本地
with open("D:\\hxy\\test\\tt.png", "wb") as fp:
    fp.write(content)
print('download done.')


